# from concurrent import futures
# import grpc
# from _grpc import example_pb2_grpc as example_pb2_grpc, example_pb2 as example_pb2
# import DWDataReader.DWDataReaderExample as reader
#
#
# class CalculatorServicer(example_pb2_grpc.CalculatorServicer):
#     def Calculate(self, request, context):
#         # 进行计算，例如加法
#         result = [request.param1 + request.param2, request.param1 - request.param2]
#         #result = reader.get_dxd_data_sample()
#         return example_pb2.Response(result=result)
#
# def serve():
#     server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
#     example_pb2_grpc.add_CalculatorServicer_to_server(CalculatorServicer(), server)
#     server.add_insecure_port('[::]:50051')
#     server.start()
#     print("Server started, listening on port 50051.")
#     server.wait_for_termination()

import grpc
from concurrent import futures
import channel_service_pb2
import channel_service_pb2_grpc
import DWDataReaderExample as reader

# 示例数据
all_channel_data = reader.get_dxd_data_sample()


class ChannelServicer(channel_service_pb2_grpc.ChannelServiceServicer):
    def GetChannelData(self, request, context):
        response = channel_service_pb2.ChannelResponse()

        for channel_name, samples in all_channel_data.items():
            channel_data = response.channels.add()
            channel_data.name = channel_name
            for sample_dict in samples:
                sample = channel_data.samples.add()
                sample.time = sample_dict["Time"]
                sample.value = sample_dict["Value"]

        return response


def serve():
    options = [('grpc.max_message_length', 100 * 1024 * 1024),
               ('grpc.max_receive_message_length', 100 * 1024 * 1024)]
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10), options=options)
    channel_service_pb2_grpc.add_ChannelServiceServicer_to_server(ChannelServicer(), server)
    server.add_insecure_port('[::]:50051')
    server.start()
    print("Server started on port 50051.")
    server.wait_for_termination()


if __name__ == '__main__':
    serve()